| Conditions | 3 |
| Paths | 3 |
| Total Lines | 61 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | const db = require("../db/database.js"); |
||
| 133 | (err, rows) => { |
||
| 134 | if (err) { |
||
| 135 | return res.status(500).json({ |
||
| 136 | errors: { |
||
| 137 | status: 500, |
||
| 138 | source: "/login", |
||
| 139 | title: "Database error", |
||
| 140 | detail: err.message |
||
| 141 | } |
||
| 142 | }); |
||
| 143 | } |
||
| 144 | |||
| 145 | if (rows === undefined) { |
||
| 146 | return res.status(401).json({ |
||
| 147 | errors: { |
||
| 148 | status: 401, |
||
| 149 | source: "/login", |
||
| 150 | title: "User not found", |
||
| 151 | detail: "User with provided email not found." |
||
| 152 | } |
||
| 153 | }); |
||
| 154 | } |
||
| 155 | |||
| 156 | const user = rows; |
||
| 157 | |||
| 158 | bcrypt.compare(password, user.password, (err, result) => { |
||
| 159 | if (err) { |
||
| 160 | return res.status(500).json({ |
||
| 161 | errors: { |
||
| 162 | status: 500, |
||
| 163 | source: "/login", |
||
| 164 | title: "bcrypt error", |
||
| 165 | detail: "bcrypt error" |
||
| 166 | } |
||
| 167 | }); |
||
| 168 | } |
||
| 169 | |||
| 170 | if (result) { |
||
| 171 | let payload = { api_key: user.apiKey, email: user.email }; |
||
| 172 | let jwtToken = jwt.sign(payload, config.secret, { expiresIn: '24h' }); |
||
| 173 | |||
| 174 | return res.json({ |
||
| 175 | data: { |
||
| 176 | type: "success", |
||
| 177 | message: "User logged in", |
||
| 178 | user: payload, |
||
| 179 | token: jwtToken |
||
| 180 | } |
||
| 181 | }); |
||
| 182 | } else { |
||
| 183 | return res.status(401).json({ |
||
| 184 | errors: { |
||
| 185 | status: 401, |
||
| 186 | source: "/login", |
||
| 187 | title: "Wrong password", |
||
| 188 | detail: "Password is incorrect." |
||
| 189 | } |
||
| 190 | }); |
||
| 191 | } |
||
| 192 | }); |
||
| 193 | }); |
||
| 194 | } |
||
| 290 |